import request, { type Response } from "@/utils/request";
import { type MerchantInfoType, type AgentInfoType, type UserInfo } from "@/api/system/user";

const AUTH_BASE_URL = "/api";

const AuthAPI = {
  /** 登录接口*/
  login(data: LoginData) {
    const formData = new FormData();
    formData.append("username", data.username);
    formData.append("password", data.password);
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/token/`,
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /** 生成微信登录二维码接口*/
  wxBizLoginPrepare() {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/wx/biz/login/prepare/`,
      method: "get",
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /** 微信登录轮询获取token*/
  wxBizLoginToken(data: any) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/wx/biz/login/token/`,
      method: "post",
      data: data,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /** 用户角色信息 **/
  userInfo() {
    return request<any, UserInfo>({
      url: `${AUTH_BASE_URL}/userinfo/`,
      method: "get",
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /** 用户角色信息 **/
  userGroups() {
    return request({
      url: `${AUTH_BASE_URL}/userinfo/groups/`,
      method: "get",
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },

  /** 刷新 token 接口*/
  refreshToken(refreshToken: string) {
    return request<any, RefreshTokenResult>({
      url: `${AUTH_BASE_URL}/token/refresh/`,
      method: "post",
      data: { refresh: refreshToken },
      headers: {
        Authorization: "no-auth",
      },
    });
  },

  /** 注销接口*/
  logout() {
    return request({
      url: `${AUTH_BASE_URL}/logout`,
      method: "delete",
    });
  },

  /** 提交申请接口*/
  supplierApply(data: ApplyData) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/supplier/apply/`,
      method: "post",
      data: data,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  // 获取当前供应商信息
  supplierInfo() {
    return request({
      url: `${AUTH_BASE_URL}/supplier/current/`,
      method: "get",
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  // 获取当前供应商申请信息
  supplierApplyInfo() {
    return request({
      url: `${AUTH_BASE_URL}/supplier/apply/query/`,
      method: "get",
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },

  // 获取当前用户的商户信息
  merchantInfo() {
    return request<any, Response<{ is_merchant: boolean; info: MerchantInfoType }>>({
      url: `${AUTH_BASE_URL}/merchtants/current/`,
      method: "get",
    });
  },

  // 获取当前代理商信息
  agentInfo() {
    return request<any, Response<{ is_agent: boolean; info: AgentInfoType }>>({
      url: `${AUTH_BASE_URL}/agent/current/`,
      method: "get",
    });
  },

  // 绑定邀请码
  bindInviteCode(code: string) {
    return request<any, any>({
      url: `${AUTH_BASE_URL}/userinfo/bind/from_code/`,
      method: "post",
      data: { code: code },
    });
  },

  // 修改密码
  changePassword(data: ChangePaswwordForm) {
    return request<ChangePaswwordForm, any>({
      url: `${AUTH_BASE_URL}/pwd/change/`,
      method: "post",
      data,
    });
  },
};

export default AuthAPI;

export type ChangePaswwordForm = {
  /**
   * 确认密码
   */
  cfm_pwd: string;
  /**
   * 新密码
   */
  new_pwd: string;
  /**
   * 旧密码
   */
  old_pwd: string;
  [property: string]: any;
};

/** 登录请求参数 */
export interface LoginData {
  /** 用户名 */
  username: string;
  /** 密码 */
  password: string;
  /** 验证码缓存key */
  // captchaKey: string;
  /** 验证码 */
  // captchaCode: string;
}

export interface ApplyData {
  password?: string;
  name: string;
  contact_name: string;
  contact_phone: string;
  contact_email: string;
  business_license_url: string;
  id_card_1: string;
  id_card_2: string;
  agent?: string;
}

/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  access: string;
  /** 刷新令牌 */
  refresh: string;
  /** 过期时间(秒) */
  expire: number;
}

export interface RefreshTokenResult {
  token: string;
}

/** 验证码响应 */
// export interface CaptchaResult {
/** 验证码缓存key */
// captchaKey: string;
/** 验证码图片Base64字符串 */
// captchaBase64: string;
// }
